package com.qkjia.srb.core.controller.api;

import com.qkjia.common.exception.Assert;
import com.qkjia.common.result.R;
import com.qkjia.common.result.ResponseEnum;
import com.qkjia.common.utils.RegexValidateUtils;
import com.qkjia.srb.base.util.JwtUtils;
import com.qkjia.srb.core.pojo.vo.LoginVO;
import com.qkjia.srb.core.pojo.vo.RegisterVO;
import com.qkjia.srb.core.pojo.vo.UserInfoVO;
import com.qkjia.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *  @author 赵乾坤
 *  @since 2021-11-10 2:07
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
//@CrossOrigin//跨域
public class UserInfoApiController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisTemplate redisTemplate;
    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();
        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);
        //校验验证码
        String codeGen = (String)redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        //CODE_ERROR(-206, "验证码不正确"),
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);
        //注册
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    /**
     * HttpServletRequest 为了获取数据库字段的ip
     * @param loginVO
     * @param request
     * @return
     */
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        //   MOBILE_NULL_ERROR(-202, "手机号码不能为空"),
        Assert.notEmpty(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        //   PASSWORD_NULL_ERROR(204, "密码不能为空"),
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);
        // 返回发送请求的客户端或最后一个代理的 Internet 协议 (IP) 地址。
        String ip = request.getRemoteAddr();
        // 返给前端他需要的对象
        UserInfoVO userInfoVO = userInfoService.login(loginVO,ip);
        return R.ok().data("userInfo", userInfoVO);

    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);
        if(result){
            return R.ok();
        }else{
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(
            @ApiParam(name = "mobile",value = "手机号",example = "13881888888")
            @PathVariable("mobile") String mobile) {
        return userInfoService.checkMobile(mobile);
    }
}